package com.bhs.sansonglogistics.dialog;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.bhs.sansonglogistics.R;
import com.bhs.sansonglogistics.base.BaseActivity;
import com.bhs.sansonglogistics.bean.LogisticsParkBean;
import com.bhs.sansonglogistics.bean.LogisticsParkDataBean;
import com.bhs.sansonglogistics.net.NetCallBack;
import com.bhs.sansonglogistics.utils.MyUtils;
import com.bhs.sansonglogistics.utils.ToastUtil;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.google.gson.Gson;

import okhttp3.ResponseBody;
import retrofit2.Response;

/**
 * 所属园区
 */
public class BottomAffiliationDialog extends Dialog implements NetCallBack, View.OnClickListener {


    private TextView mTvTitle;
    private RecyclerView mRvList;

    private BaseActivity activity;
    private BaseQuickAdapter<LogisticsParkBean, BaseViewHolder> mAdapter;
    private String park_city;
    private int page = 1;


    public BottomAffiliationDialog(@NonNull BaseActivity context, String park_city) {
        super(context, R.style.MyBottomDialogStyle);
        this.activity = context;
        this.park_city = park_city;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_bottom_select_lien);
        Window window = getWindow();
        //底部弹出的Dialog
        window.setGravity(Gravity.BOTTOM);
        //设置动画
        window.setWindowAnimations(R.style.main_menu_animstyle);
        WindowManager.LayoutParams attributes = window.getAttributes();

        //保证按钮水平满屏
        attributes.width = ViewGroup.LayoutParams.MATCH_PARENT;
        attributes.height = MyUtils.getScreenHeight(getContext()) * 5 / 10;

        //设置显示位置
        window.setAttributes(attributes); //将属性设置给窗体
        initView();
    }

    private void initView() {
        findViewById(R.id.iv_close).setOnClickListener(this);
        mTvTitle = findViewById(R.id.tv_title);
        mRvList = findViewById(R.id.rv_list);
        mTvTitle.setText("所属园区");
        mAdapter = new BaseQuickAdapter<LogisticsParkBean, BaseViewHolder>(R.layout.item_special_line) {
            @Override
            protected void convert(@NonNull BaseViewHolder helper, LogisticsParkBean item) {
                helper.setText(R.id.tv_refuse, item.getPark_name());
                helper.setImageResource(R.id.iv_refuse, item.isChecked() ? R.mipmap.ic_checked : R.mipmap.ic_unchecked);
            }
        };
        mRvList.setAdapter(mAdapter);
        mRvList.setLayoutManager(new LinearLayoutManager(activity));

        mAdapter.setEmptyView(LayoutInflater.from(getContext()).inflate(R.layout.layout_empty2, null));
        mAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                for (LogisticsParkBean datum : mAdapter.getData()) {
                    datum.setChecked(false);
                }
                mAdapter.getData().get(position).setChecked(true);
                mAdapter.notifyDataSetChanged();
                myClickListener.onClick(String.valueOf(mAdapter.getData().get(position).getPark_id()), mAdapter.getData().get(position).getPark_name());
                dismiss();
            }
        });

        mAdapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
            @Override
            public void onLoadMoreRequested() {
                page++;
                loadData();
            }
        }, mRvList);
        loadData();
    }

    private void loadData() {
        activity.networkRequest(activity.netApi.getLogisticsParkList(park_city, page), this);
    }

    @SuppressLint("NotifyDataSetChanged")
    @Override
    public void onResponse(int requestCode, String json, Response<ResponseBody> response) throws Exception {
        LogisticsParkDataBean bean = new Gson().fromJson(json, LogisticsParkDataBean.class);
        if (bean.isStatus()) {
            if (page == 1) {
                mAdapter.setNewData(bean.getData().getList());
                if (mAdapter.getData().size() > 0) {
                    mAdapter.getData().get(0).setChecked(true);
                    mAdapter.notifyDataSetChanged();
                }
            } else {
                mAdapter.loadMoreComplete();
                mAdapter.addData(bean.getData().getList());
            }
            if (mAdapter.getData().size() >= bean.getData().getTotal_count()) {
                mAdapter.loadMoreEnd();
            }

        } else {
            ToastUtil.show(bean.getMsg());
        }
    }

    @Override
    public void onFailure(int requestCode) {

    }

    private MyClickListener2 myClickListener;

    public void setMyClickListener(@Nullable MyClickListener2 l) {
        this.myClickListener = l;
    }

    @Override
    public void onClick(View view) {
        dismiss();
    }
}
